import prompt from '@system.prompt';
import router from '@system.router';
import http from '@ohos.net.http';
export default {
    data: {
        Info:"",
        title: 'World',
        pages:0,
        commodity:[],
        more:[]
    },
    back() {
        router.back()
    },
    onInit(){
        this.Info = router.getParams().searchInfo
        console.log("------->")
        console.log(this.Info)
        this.pages=0
        this.initItem()

    },
    getPage(){
        console.log("到底了")
        this.moreItem()
        prompt.showToast({
            message:"加载成功",
            duration:2000
        })
    },
    refresh(){
        this.initItem()
        prompt.showToast({
            message:"刷新成功",
            duration:2000
        })
    },
    initItem(){
        this.pages=0
        // 每一个httpRequest对应一个http请求任务，不可复用
        let httpRequest = http.createHttp();

        httpRequest.request(
        // 填写http请求的url地址，可以带参数也可以不带参数。URL地址需要开发者自定义。GET请求的参数可以在extraData中指定
        this.$app.$def.getItemSearchUrl()+"?searchInfo="+this.Info+"&start="+this.pages,
            {
                method: 'GET',
            }, (err, data) => {
            if (!err) {
                // data.result为http响应内容，可根据业务需要进行解析
                console.info('Result:' + data.result);
                console.log("--------->初始化")
                this.commodity = null
                this.$app.$def.globalData.jsonResult = JSON.parse(data.result);
                this.commodity = this.$app.$def.globalData.jsonResult.info;
                console.info('code:' + this.$app.$def.globalData.jsonResult.code);
                console.info('msg:' + this.$app.$def.globalData.jsonResult.msg);
            } else {
                console.info('error:' + err.data);
            }
        }
        );
    },
    moreItem(){
        this.pages = this.pages+1
        // 每一个httpRequest对应一个http请求任务，不可复用
        let httpRequest = http.createHttp();

        httpRequest.request(
        // 填写http请求的url地址，可以带参数也可以不带参数。URL地址需要开发者自定义。GET请求的参数可以在extraData中指定
            this.$app.$def.getItemSearchUrl()+"?searchInfo="+this.Info+"&start="+this.pages,
            {
                method: 'GET',
            }, (err, data) => {
            if (!err) {
                // data.result为http响应内容，可根据业务需要进行解析
                console.info('Result:' + data.result);
                console.log("--------->加载更多")

                //this.$app.$def.globalData.jsonResult = JSON.parse(data.result);
                this.$app.$def.globalData.jsonResult = JSON.parse(data.result);
                this.more = this.$app.$def.globalData.jsonResult.info
                //console.info("<>>>>>>"+data.result)
                for(var i = 0; i < this.more.length; i++){
                    this.commodity.push(this.more[i])
                }

            } else {
                console.info('error:' + err.data);
            }
        }
        );
    },
    onjump(index){
        router.push({
            uri: "pages/detail/detail",
            params: {
                commodity: this.commodity[index]
            }
        })
    }
}
